home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / k_r.arc / K&R_CH5.C < prev    next >
Text File  |  1985-11-05  |  11KB  |  1,125 lines

  1. pr 5.*
  2.  
  3.  
  4. Aug  3 09:13 1984  5.12pdemo.c Page 1
  5.  
  6.  
  7. main()          /* demonstration of pointers */
  8. {
  9.   int x, y;
  10.   int *px;      /* declare px pointer to type int */
  11.  
  12.   x = 5;
  13.   px = &x;      /* initialize px to address of x */
  14.   y = *px;      /* initialize y to contents of address
  15.                    contained in px */
  16.   printf("addresses of x, y, px are...\n\t%8o, %8o, %8o\n",
  17.                 &x, &y, &px);
  18.   printf("values of x, y, px, *px are...\n\t%8d, %8d, %8o, %8d\n",
  19.                 x, y, px, *px);
  20. y = 12;
  21. px = &y;
  22.   printf("addresses of x, y, px are...\n\t%8o, %8o, %8o\n",
  23.                 &x, &y, &px);
  24.   printf("values of x, y, px, *px are...\n\t%8d, %8d, %8o, %8d\n",
  25.                 x, y, px, *px);
  26. }
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. Jul 27 17:12 1984  5.14swap_ng.c Page 1
  71.  
  72.  
  73. /* swapping values - wrong way */
  74.  
  75. #include "getint.f"     /* assumes getint() in
  76.                            file getint.f in wd */
  77. main()
  78. {       int a = getint(), b = getint();
  79.  
  80.         printf("original a, b are\t%6d, %6d\n", a, b);
  81.         swap_ng(a, b);
  82.         printf("swapped a, b are\t%6d, %6d\n", a, b);
  83. }
  84. swap_ng(x, y)           /* call by value */
  85. int x, y;
  86. {       int tmp;
  87.         tmp = x;
  88.         x = y;
  89.         y = tmp;
  90. }
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. Jul 27 17:12 1984  5.17swap.c Page 1
  137.  
  138.  
  139. /* swapping values - correct way */
  140.  
  141. #include "getint.f"     /* assumes getint() in
  142.                            file getint.f in wd */
  143. main()
  144. {       int a = getint(), b = getint();
  145.  
  146.         printf("original a, b are\t%6d, %6d\n", a, b);
  147.         swap(&a, &b);
  148.         printf("swapped a, b are\t%6d, %6d\n", a, b);
  149. }
  150. swap(px, py)            /* call by reference */
  151. int *px, *py;           /* px, py are pointers to type int */
  152. {       int tmp;
  153.         tmp = *px;
  154.         *px = *py;
  155.         *py = tmp;
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. Jul 27 17:12 1984  5.20ap.c Page 1
  203.  
  204.  
  205. /* demonstration of similarity between arrays and pointers */
  206.  
  207. main()
  208. {       static char ar[] = "test";
  209.         char *par;
  210.         int i;
  211.         par = ar;               /* "&" not used, WHY? */
  212.  
  213.         printf("array is\t\"%s\"\n", ar);
  214.         printf("pointer is\t\"%s\"\n", par);
  215.         printf("array\tpointer\n");
  216.         for (i = 0; i < (sizeof(ar) - 1); ++i)
  217.                 printf("  %c\t  %c\n", ar[i], *par++);
  218. }
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. Jul 27 17:12 1984  5.26pcs.c Page 1
  269.  
  270.  
  271. /* pointers to "character strings" */
  272.  
  273. main()
  274. {       static char arr1[] = { 'I', ' ', 'u', 's', 'e', '\0' };
  275.         char *ptr1 = arr1;
  276.         char *ptr2 = "pointers.";
  277.  
  278.         printf("%s %s\n", ptr1, ptr2);
  279. }
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. Jul 27 17:12 1984  5.28strln_p.c Page 1
  335.  
  336.  
  337. /* pointer version of strln() */
  338.  
  339. #define MAX 256
  340. #include <stdio.h>      /* getline() needs EOF */
  341. #include "getline.f"    /* assumes getline() is in
  342.                            file getline.f in wd */
  343. main()  /* test strln_p() */
  344. {       char stor[MAX];
  345.         getline(stor, MAX);
  346.         printf("string length is %d\n", strln_p(stor));
  347. }
  348. strln_p(ptr)    /* return length of char string pointed to by ptr;
  349.                 /* include null at end in count */
  350. char *ptr;
  351. {       register length = 0;
  352.         do
  353.         {       ++length;
  354.         } while (*ptr++ != '\0');       /* ++ increments ptr by 1 */
  355.         return (length);
  356. }
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. Jul 27 17:12 1984  5.30strcpy.c Page 1
  401.  
  402.  
  403. /* copying strings - array version */
  404.  
  405. #define MAX 256
  406. #include <stdio.h>      /* getline() needs EOF */
  407. #include "getline.f"    /* assumes getline() is in
  408.                            file getline.f in wd */
  409. main() /* test strcpy() */
  410. {       char src[MAX], dest[MAX];
  411.         getline(src, MAX);
  412.         strcpy(dest, src);
  413.         printf("src, dest...\n%s%s", src, dest);
  414. }
  415. strcpy(d, s)    /* copy string in s[] into d[] */
  416. char d[], s[];
  417. {       register i;
  418.         for (i = 0; d[i] = s[i]; ++i)
  419.                 ;
  420. }
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466. Jul 27 17:12 1984  5.33strcpy_p.c Page 1
  467.  
  468.  
  469. /* copying strings - pointer version */
  470.  
  471. #define MAX 256
  472. #include <stdio.h>      /* getline() needs EOF */
  473. #include "getline.f"    /* assumes getline() is in
  474.                            file getline.f in wd */
  475. main() /* test strcpy_p() */
  476. {       char src[MAX], dest[MAX];
  477.         getline(src, MAX);
  478.         strcpy_p(dest, src);
  479.         printf("src, dest...\n%s%s", src, dest);
  480. }
  481. strcpy_p(d, s)  /* copy string *s to *d */
  482. char *d, *s;
  483. {       
  484.         while (*d++ = *s++)
  485.                 ;
  486. }
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532. Jul 27 17:12 1984  5.34strcmp.c Page 1
  533.  
  534.  
  535. /* comparing strings with pointers */
  536.  
  537. #define MAX 256
  538. #include <stdio.h>
  539. #include "getline.f"
  540. main()  /* test strcmp() */
  541. {       char str1[MAX], str2[MAX];
  542.         register ret;
  543.         printf("input 1st string: "); getline(str1, MAX);
  544.         printf("input 2nd string: "); getline(str2, MAX);
  545.         if (!(ret = strcmp(str1, str2)))
  546.                 printf("they are equal\n");
  547.         else    printf("no match, char %d\n", ret);
  548. }
  549. strcmp(s1, s2)  /* compare 2 strings;
  550.                 /* if match return 0 (else char count) */
  551. char    *s1, *s2;
  552. {       register i;
  553.         for (i = 1; *s1 == *s2; ++i)
  554.                 if (*s1++ == '\0')
  555.                         return (0);
  556.                 else s2++;
  557.         return (i);
  558. }
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598. Jul 27 17:12 1984  5.38mda1.c Page 1
  599.  
  600.  
  601. #define D1 3
  602. #define D2 5
  603. int a[D1][D2];  /* two-dimensional array */
  604. main()          /* demonstration of multi-dimensional array */
  605. { register i, j;
  606.  
  607.   for (i = 0; i < D1; ++i)
  608.     for (j = 0; j < D2; ++j)
  609.     { a[i][j] = (i * D2) + j + 1;
  610.       printf("a[%d][%d] adr, value is %o, %2d\n",
  611.                 i, j, &a[i][j], a[i][j]);
  612.     }
  613. }
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664. Jul 27 17:12 1984  5.40mda2.c Page 1
  665.  
  666.  
  667. #define D1 3
  668. #define D2 5
  669. int a[][D2] =   { {  1,  2,  3,  4,  5 },
  670.                   {  6,  7,  8,  9, 10 },
  671.                   { 11, 12, 13, 14, 15 }
  672.                 };      /* compiler initialized */
  673. main()  /* two-dimensional array; another version */
  674. { register i, j;
  675.  
  676.   for (i = 0; i < D1; ++i)
  677.     for (j = 0; j < D2; ++j)
  678.       printf("a[%d][%d] adr, value is %o, %2d\n",
  679.                 i, j, &a[i][j], a[i][j]);
  680. }
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730. Jul 27 17:12 1984  5.44aop.c Page 1
  731.  
  732.  
  733. main()  /* demonstration of an array of pointers */
  734. {       register i;
  735.         static char *arp[] =    { "I",
  736.                                   "NEED",
  737.                                   "HELP",
  738.                                   ""
  739.                                 };
  740.         for (i = 0; *arp[i]; ++i)
  741.                 printf("%s ", arp[i]);  
  742.         putchar('\n');
  743. }
  744.  
  745.  
  746.  
  747.  
  748.  
  749.  
  750.  
  751.  
  752.  
  753.  
  754.  
  755.  
  756.  
  757.  
  758.  
  759.  
  760.  
  761.  
  762.  
  763.  
  764.  
  765.  
  766.  
  767.  
  768.  
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796. Jul 27 17:12 1984  5.49argdemo.c Page 1
  797.  
  798.  
  799. /* demonstration of arguments passed to main() */
  800.  
  801. main(argc, argv, envp)          /* three args to main() */
  802. int     argc;                   /* argc is an int */
  803. char    *argv[], *envp[];       /* argv & envp are arrays
  804.                                 /* of character pointers */
  805. {       register i;
  806.  
  807.         printf("argc is %d\n", argc);
  808.         for (i = 0; i < argc; ++i)
  809.                 printf("*argv[%d] is %s\n", i, argv[i]);
  810.         for (i = 0; envp[i]; ++i)
  811.                 printf("*envp[%d] is %s\n", i, envp[i]);
  812. }
  813.  
  814.  
  815.  
  816.  
  817.  
  818.  
  819.  
  820.  
  821.  
  822.  
  823.  
  824.  
  825.  
  826.  
  827.  
  828.  
  829.  
  830.  
  831.  
  832.  
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862. Jul 27 17:12 1984  5.50echo1.c Page 1
  863.  
  864.  
  865. main(argc, argv)        /* emulation of basic echo(1) command;
  866.                         /* first version, array notation */
  867. int     argc;
  868. char    *argv[];
  869. {
  870.   register i;
  871.  
  872.   for (i = 1; i < argc; ++i)
  873.     printf("%s%c", argv[i], (i < argc - 1)? ' ': '\n');
  874. }
  875.  
  876.  
  877.  
  878.  
  879.  
  880.  
  881.  
  882.  
  883.  
  884.  
  885.  
  886.  
  887.  
  888.  
  889.  
  890.  
  891.  
  892.  
  893.  
  894.  
  895.  
  896.  
  897.  
  898.  
  899.  
  900.  
  901.  
  902.  
  903.  
  904.  
  905.  
  906.  
  907.  
  908.  
  909.  
  910.  
  911.  
  912.  
  913.  
  914.  
  915.  
  916.  
  917.  
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928. Jul 27 17:12 1984  5.53echo2.c Page 1
  929.  
  930.  
  931. main(argc, argv)        /* emulation of basic echo(1) command;
  932.                         /* second version, pointer notation */
  933. int     argc;
  934. char    **argv;         /* same as *argv[] */
  935. {
  936.   while (--argc > 0)
  937.     printf("%s%c", *++argv, (argc > 1)? ' ': '\n');
  938. }
  939.  
  940.  
  941.  
  942.  
  943.  
  944.  
  945.  
  946.  
  947.  
  948.  
  949.  
  950.  
  951.  
  952.  
  953.  
  954.  
  955.  
  956.  
  957.  
  958.  
  959.  
  960.  
  961.  
  962.  
  963.  
  964.  
  965.  
  966.  
  967.  
  968.  
  969.  
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994. Jul 27 17:12 1984  5.60frp.c Page 1
  995.  
  996.  
  997. /* demonstration of function returning pointer */
  998.  
  999. main()                  /* test new version of getint() */
  1000. {
  1001.   int *getintp();       /* getintp() returns pointer to type int */
  1002.  
  1003.   printf("%d\n", *getintp());
  1004. }
  1005. int *getintp()  /* function to convert ASCII
  1006.                 /* input from terminal to an int;
  1007.                 /* return pointer to int */
  1008. { int   c, sign = 1, num = 0;
  1009.  
  1010.   printf("input integer: ");
  1011.   if    ((c = getchar()) == '-')
  1012.         sign = -1;
  1013.   else if (c >= '0' && c <= '9')
  1014.         num = (c - '0');
  1015.   else
  1016.         return (&num);
  1017.   while ((c = getchar()) >= '0' && c <= '9')
  1018.         num = num * 10 + (c - '0');
  1019.   num *= sign;
  1020.   return (&num);
  1021. }
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.  
  1029.  
  1030.  
  1031.  
  1032.  
  1033.  
  1034.  
  1035.  
  1036.  
  1037.  
  1038.  
  1039.  
  1040.  
  1041.  
  1042.  
  1043.  
  1044.  
  1045.  
  1046.  
  1047.  
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060. Jul 27 17:12 1984  5.63pfi.c Page 1
  1061.  
  1062.  
  1063. /* demonstration of pointer to function */
  1064.  
  1065. main()          /* test inter() */
  1066. {
  1067.   int getint(); /* getint() must be declared */
  1068.  
  1069.   printf("%d\n", inter(getint));
  1070. }
  1071. inter(pfi)      /* call function via pointer arg */
  1072. int (*pfi)();   /* pfi is pointer to function
  1073.                 /* returning type int */
  1074. {
  1075.   return ((*pfi)());    /* (*pfi)() is a function call */
  1076. }
  1077. #include "getint.f"     /* assumes getint() is in
  1078.                         /* file getint.f in wd */
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093.  
  1094.  
  1095.  
  1096.  
  1097.  
  1098.  
  1099.  
  1100.  
  1101.  
  1102.  
  1103.  
  1104.  
  1105.  
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.